package com.icarsaas.saas.tenant.system.service.impl;


import com.icarsaas.saas.tenant.system.model.FileUpload;
import com.icarsaas.saas.tenant.system.repository.FileUploadRepository;
import com.icarsaas.saas.tenant.system.service.UploadService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UploadServiceImpl implements UploadService {

    @Autowired
    private FileUploadRepository uploadRepository;

    /**
     * 获取文件sha1值的记录

    @Override
    public FileUpload getByMd5(String md5) {
        return uploadRepository.findByMd5(md5);
    }
     */

    /**
     * 保存文件上传
     * @param upload 文件上传实体类
     */
    @Override
    public FileUpload save(FileUpload upload){
        return uploadRepository.save(upload);
    }

    /**
     * 新增时将随机id变换成refId
     * @param ramId 随机id
     * @param refId 关联id
     */
    @Transactional(value = "tenantTransactionManager")
    public void updateRefId(String ramId, String refId) {
        if (StringUtils.isNotEmpty(ramId) && StringUtils.isNotEmpty(refId)) {
            String sql = "update sys_file_upload set ref_id='" + refId + "' where ref_id='" + ramId + "'";
            uploadRepository.updateBySql(sql);
        }
    }

    @Override
    public FileUpload getById(String id) {
        return uploadRepository.findById(id).orElse(null);
    }
}

